package com.junqi.live.room.application.controller;

import com.junqi.common.entity.AjaxResult;
import com.junqi.common.entity.ChatMessageDto;
import com.junqi.common.entity.Message;
import com.junqi.live.room.application.convert.ChatMessageDtoConvert;
import com.junqi.live.room.application.convert.LiveRoomDtoConvert;
import com.junqi.live.room.application.convert.LiveTypeDtoConvert;
import com.junqi.live.room.application.dto.LiveRoomDto;
import com.junqi.live.room.common.annotation.RateLimit;
import com.junqi.live.room.common.utils.LoginUtil;
import com.junqi.live.room.domain.bo.ChatMessageBo;
import com.junqi.live.room.domain.bo.LiveRoomBo;
import com.junqi.live.room.domain.service.LiveRoomDomainService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/liveRoom")
public class LiveRoomController {

    @Resource
    LiveRoomDomainService liveRoomDomainService;

    /**
     * 查询房间详情
     * @param id
     * @return
     */
    @GetMapping("/getInfo")
    public AjaxResult getInfo(@RequestParam("id") String id){
        LiveRoomBo liveRoomBo=liveRoomDomainService.getInfo(id);
        return AjaxResult.success(LiveRoomDtoConvert.INSTANCE.convertBoToDtO(liveRoomBo));
    }

    /**
     * 发送消息
     * @param request
     * @return
     */
    @PostMapping("/chat")
    @RateLimit(maxRequests = 50)
    public AjaxResult chat(@Valid @RequestBody ChatMessageDto request) {
        ChatMessageBo chatMessageBo = ChatMessageDtoConvert.INSTANCE.convertDtOToBo(request);
        Message message = liveRoomDomainService.sendMsg(chatMessageBo);
        return AjaxResult.success(message);
    }

    /**
     * 用户关注和取关直播间
     * @param roomId 直播间id
     * @param select 关注=true，取关=false
     * @return
     */
    @GetMapping("/updateCollect/{roomId}/{select}")
    public AjaxResult updateCollect(@PathVariable("roomId") String roomId,@PathVariable("select") Boolean select) {
        liveRoomDomainService.updateCollect(roomId,select);
        return AjaxResult.success();
    }

    /**
     * 查询用户关注直播间列表
     * @return
     */
    @GetMapping("/getRoomCollectList")
    public AjaxResult getRoomCollectList(){
        List<LiveRoomBo> roomList=liveRoomDomainService.getRoomCollectList();
        return AjaxResult.success(roomList.stream().map(m -> LiveRoomDtoConvert.INSTANCE.convertBoToDtO(m)).collect(Collectors.toList()));
    }

    /**
     * 查询用户开播房间详情
     * @return
     */
    @GetMapping("/getRoomUserInfo")
    public AjaxResult getRoomUserInfo(){
        LiveRoomBo liveRoomBo=liveRoomDomainService.getRoomUserInfo();
        return AjaxResult.success(LiveRoomDtoConvert.INSTANCE.convertBoToDtO(liveRoomBo));
    }


    /**
     * 开播
     * @param dto
     * @return
     */
    @PostMapping("/openPlay")
    public AjaxResult openPlay(@Valid @RequestBody LiveRoomDto dto) {
        LiveRoomBo liveRoomBo = LiveRoomDtoConvert.INSTANCE.convertDtOToBo(dto);
        LiveRoomBo liveRoomBoRespon = liveRoomDomainService.openPlay(liveRoomBo);
        return AjaxResult.success(liveRoomBoRespon);
    }

    /**
     * 关播
     * @return
     */
    @GetMapping("/closePlay")
    public AjaxResult closePlay() {
        liveRoomDomainService.closePlay();
        return AjaxResult.success();
    }
}
